package cn.iocoder.yudao.module.written.exam.controller.app.writtenexam;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
import cn.iocoder.yudao.framework.common.vo.OptionVO;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.infra.enums.MessageTypeEnum;
import cn.iocoder.yudao.module.written.exam.controller.app.writtenexam.vo.AppWrittenExamFinishReqVO;
import cn.iocoder.yudao.module.written.exam.controller.app.writtenexam.vo.AppWrittenExamPointsScopeRespVO;
import cn.iocoder.yudao.module.written.exam.controller.app.writtenexam.vo.AppWrittenExamSettingReqVO;
import cn.iocoder.yudao.module.written.exam.controller.app.writtenexam.vo.AppWrittenExamSettingRespVO;
import cn.iocoder.yudao.module.written.exam.controller.app.writtenexam.vo.AppWrittenExamStopPushAnswerReqVO;
import cn.iocoder.yudao.module.written.exam.controller.app.writtenexam.vo.llm.LlmChatReqVO;
import cn.iocoder.yudao.module.written.exam.controller.app.writtenexam.vo.message.AppWrittenExamMessageVO;
import cn.iocoder.yudao.module.written.exam.service.writtenexam.WrittenExamService;
import cn.iocoder.yudao.module.written.exam.service.writtenexammessage.WrittenExamMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "APP（有关笔试） - 笔试")
@RestController
@RequestMapping("/written-exam")
@Validated
public class AppWrittenExamController {

    @Resource
    private WrittenExamService writtenExamService;
    @Resource
    private WrittenExamMessageService writtenExamMessageService;

    /**
     * 获取笔试枚举
     *
     * @return
     */
    @GetMapping("/enums")
    @Operation(summary = "笔试枚举", description = "获取笔试枚举")
    @PermitAll
    public CommonResult<Map<String, List<OptionVO>>> getTypes() {
        return success(writtenExamService.getEnums());
    }

    /**
     * 获取桌面端开启状态
     *
     * @return
     */
    @GetMapping("/desktop-status")
    @Operation(summary = "桌面端开启状态", description = "获取桌面端开启状态")
    public CommonResult<Boolean> desktopStatus() {
        return success(writtenExamService.desktopStatus(WebFrameworkUtils.getLoginUserId()));
    }

    /**
     * 积分范围
     *
     * @return
     */
    @GetMapping("/points-scope")
    @Operation(summary = "积分范围", description = "积分范围")
    @PermitAll
    public CommonResult<AppWrittenExamPointsScopeRespVO> pointsScope() {
        return success(writtenExamService.pointsScope());
    }

    /**
     * 设置
     *
     * @param reqVO
     * @return
     */
    @PostMapping("/setting/set")
    @Operation(summary = "设置")
    public CommonResult<Boolean> settingSet(@RequestBody AppWrittenExamSettingReqVO reqVO) {
        return success(writtenExamService.settingSet(WebFrameworkUtils.getLoginUserId(), reqVO));
    }

    /**
     * 获取设置
     *
     * @return
     */
    @GetMapping("/setting/get")
    @Operation(summary = "获取设置")
    public CommonResult<AppWrittenExamSettingRespVO> settingGet() {
        return success(writtenExamService.settingGet(WebFrameworkUtils.getLoginUserId()));
    }

    /**
     * 批量上传图片文件
     *
     * @param files
     * @param conversationId
     * @param opType
     * @return
     */
    @PostMapping("/upload-files")
    @Operation(summary = "批量上传图片文件（最多3个）")
    public CommonResult<String> uploadFiles(
            @Parameter(description = "上传的文件列表（最多3个图片文件）") @RequestParam("files") MultipartFile[] files,
            @Parameter(description = "会话id") @RequestParam(name = "conversationId", required = false) String conversationId,
            @Parameter(description = "操作类型") @RequestParam(name = "opType", required = false) Integer opType) {
        return success(writtenExamService.uploadFiles(WebFrameworkUtils.getLoginUserId(), ServletUtils.getClientIP(), conversationId, files, opType));
    }

    /**
     * 发送消息
     *
     * @param reqVO
     * @return
     */
    @PostMapping("/send-message")
    @Operation(summary = "发送消息（阻塞式响应）")
    public CommonResult<String> sendMessage(
            @Parameter(description = "发送消息请求") @Valid @RequestBody LlmChatReqVO reqVO) {
        return success(writtenExamService.pushAnswerMessage(WebFrameworkUtils.getLoginUserId(), reqVO, ServletUtils.getClientIP()));
    }

    /**
     * 事件消息
     *
     * @return
     */
    @GetMapping("/event-message")
    @Operation(summary = "事件消息")
    public CommonResult<Boolean> eventMessage(@RequestParam("type") String type) {
        MessageTypeEnum messageTypeEnum = MessageTypeEnum.getByType(type);
        if (Objects.isNull(messageTypeEnum)) {
            return success(false);
        }
        writtenExamMessageService.push(WebFrameworkUtils.getLoginUserId(), messageTypeEnum.getType(), new AppWrittenExamMessageVO());
        return success(true);
    }

    /**
     * 停止推送答案
     *
     * @param reqVO
     * @return
     */
    @PostMapping("/stop-push-answer")
    @Operation(summary = "停止推送答案")
    public CommonResult<Boolean> stopPushAnswer(
            @Parameter(description = "停止推送答案") @Valid @RequestBody AppWrittenExamStopPushAnswerReqVO reqVO) {
        return success(writtenExamService.stopPushAnswer(reqVO));
    }

    /**
     * 笔试结束
     *
     * @param reqVO
     * @return
     */
    @PostMapping("/finish")
    @Operation(summary = "笔试结束")
    public CommonResult<Boolean> finish(
            @Parameter(description = "笔试结束") @Valid @RequestBody AppWrittenExamFinishReqVO reqVO) {
        reqVO.setUserId(WebFrameworkUtils.getLoginUserId());
        return success(writtenExamService.finish(reqVO));
    }

}